package com.hhkj.payment.wechat.common.component.provider;

import com.hhkj.payment.wechat.common.conf.WechatConf;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;

/**
 * ConfProvider
 *
 * @author HHKJ-SERVER-ZJ
 * @since 2023/11/15 8:55
 */
public class ConfProvider {

    private static Config RSAAutoCertificateConfig;

    public synchronized static Config getRSAAutoCertificateConfig() {
        if (RSAAutoCertificateConfig == null) {
            RSAAutoCertificateConfig = createRSAAutoCertificateConfig();
        }
        return RSAAutoCertificateConfig;
    }

    /**
     * 创建配置信息
     *
     * @return 配置信息
     */
    private static Config createRSAAutoCertificateConfig() {
        return new RSAAutoCertificateConfig.Builder()
                .merchantId(WechatConf.getMchId())
                // 使用 com.wechat.pay.java.core.util 中的函数从本地文件中加载商户私钥，商户私钥会用来生成请求的签名
                .privateKeyFromPath(WechatConf.getApiClientCertPemPath())
                // .privateKey(WechatConf.privateKey)
                .merchantSerialNumber(WechatConf.getMchSerialNo())
                .apiV3Key(WechatConf.getApiV3Key())
                .build();
    }
}
